<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS 3D Rotation Example</title>
    <style>
        .container {
            margin-top: 100px;
            width: 200px;
            height: 200px;
            margin: 100px auto;
            perspective: 1000px; /* 创建透视效果 */
        }

        .cube {
            width: 100%;
            height: 100%;
            position: relative;
            transform-style: preserve-3d; /* 保留 3D 空间 */
            transition: transform 2s; /* 添加过渡动画 */
        }

        .face {
            position: absolute;
            width: 200px;
            height: 200px;
            border: 2px solid #333;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            font-weight: bold;
            opacity: 0.8;
        }

        .front { background: rgba(255, 0, 0, 0.7); transform: translateZ(100px); }
        .back { background: rgba(0, 255, 0, 0.7); transform: rotateY(180deg) translateZ(100px); }
        .right { background: rgba(0, 0, 255, 0.7); transform: rotateY(90deg) translateZ(100px); }
        .left { background: rgba(255, 255, 0, 0.7); transform: rotateY(-90deg) translateZ(100px); }
        .top { background: rgba(255, 0, 255, 0.7); transform: rotateX(90deg) translateZ(100px); }
        .bottom { background: rgba(0, 255, 255, 0.7); transform: rotateX(-90deg) translateZ(100px); }

        /* 旋转按钮样式 */
        .controls {
            text-align: center;
            margin-top: 20px;
        }

        button {
            padding: 10px 15px;
            margin: 5px;
            font-size: 16px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="container">
        <div id="cube" class="cube">
            <div class="face front">前面</div>
            <div class="face back">后面</div>
            <div class="face right">右面</div>
            <div class="face left">左面</div>
            <div class="face top">顶面</div>
            <div class="face bottom">底面</div>
        </div>
    </div>

    <div class="controls">
        <button onclick="rotateCube('x')">绕 X 轴旋转</button>
        <button onclick="rotateCube('y')">绕 Y 轴旋转</button>
        <button onclick="rotateCube('z')">绕 Z 轴旋转</button>
        <button onclick="rotateCube('xyz')">绕 3D 轴旋转</button>
        <button onclick="resetCube()">重置</button>
    </div>

    <script>
        let rotationX = 0;
        let rotationY = 0;
        let rotationZ = 0;
        const cube = document.getElementById('cube');

        function rotateCube(axis) {
            switch(axis) {
                case 'x':
                    rotationX += 45;
                    cube.style.transform = `rotate3d(1, 0, 0, ${rotationX}deg)`;
                    break;
                case 'y':
                    rotationY += 45;
                    cube.style.transform = `rotate3d(0, 1, 0, ${rotationY}deg)`;
                    break;
                case 'z':
                    rotationZ += 45;
                    cube.style.transform = `rotate3d(0, 0, 1, ${rotationZ}deg)`;
                    break;
                case 'xyz':
                    rotationX += 30;
                    rotationY += 30;
                    rotationZ += 30;
                    cube.style.transform = `rotate3d(1, 1, 1, ${rotationX}deg)`;
                    break;
            }
        }

        function resetCube() {
            rotationX = 0;
            rotationY = 0;
            rotationZ = 0;
            cube.style.transform = 'rotate3d(0, 0, 0, 0deg)';
        }
    </script>
</body>
</html>